package com.bookmanagement.service.impl;

import com.bookmanagement.VO.BookRecordVO;
import com.bookmanagement.entity.Book;
import com.bookmanagement.entity.User;
import com.bookmanagement.mapper.BookMapper;
import com.bookmanagement.mapper.BorrowRecordMapper;
import com.bookmanagement.mapper.UserMapper;
import com.bookmanagement.result.PageResult;
import com.bookmanagement.service.RecordService;
import com.bookmanagement.utils.BaseContext;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 17355
 */
@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private BorrowRecordMapper borrowRecordMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BookMapper bookMapper;
    @Override
    public PageResult getRecord() {
        String currentId = BaseContext.getCurrentId();
        User user=userMapper.queryByEmail(currentId);
        Integer userId = user.getId();
        List<BookRecordVO> recordList=borrowRecordMapper.getBatchRecordByUserId(userId);
        recordList.forEach(record -> {
            Book book=bookMapper.getById(record.getBookId());
            record.setAuthor(book.getAuthor());
            record.setTitle(book.getTitle());
            record.setCategoryId(book.getCategoryId());
        });
        return PageResult.builder()
                .total(recordList.size())
                .records(recordList)
                .build();
    }

    @Override
    public void deleteRecord(Integer id) {
        borrowRecordMapper.deleteRecord(id);
    }

    @Override
    public PageResult getCurrentRecord() {
        String currentId = BaseContext.getCurrentId();
        User user=userMapper.queryByEmail(currentId);
        Integer id = user.getId();
        List<BookRecordVO> recordList=borrowRecordMapper.getCurrentRecord(id);
        return PageResult.builder()
                .total(recordList.size())
                .records(recordList)
                .build();
    }
}
